# Create your views here.


from django.shortcuts import render_to_response,HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.template import RequestContext
from django.conf.urls.defaults import *
from timtalks.main.models import AppModel,AppForm, LoginForm, UserRSVP, RSVPForm
from timtalks.filetransfers.api import serve_file
from django.shortcuts import get_object_or_404
from django.contrib.auth import authenticate, login, logout
import datetime
import locale


def index(request):
    return render_to_response('index.html',context_instance=RequestContext(request))

def story(request):
    return render_to_response('story.html',context_instance=RequestContext(request))

def contact(request):
    return render_to_response('contact.html',context_instance=RequestContext(request))

def sponsors(request):
    return render_to_response('sponsors.html',context_instance=RequestContext(request))
def speakers(request):
    return render_to_response('speakers.html',context_instance=RequestContext(request))

def rsvp(request):
    response = 0
    if request.method == 'POST':
        form = RSVPForm(request.POST, request.FILES)
        if form.is_valid():
            newuser = UserRSVP()
            newuser.firstname = form.cleaned_data['firstname']
            newuser.lastname = form.cleaned_data['lastname']
            newuser.guest_num = form.cleaned_data['guest_num']
            newuser.datestamp = datetime.datetime.now()
            newuser.email = form.cleaned_data['email']
            newuser.save()
            
            response = 1
            return render_to_response('rsvp.html',{'form':form,'response':response,'user':newuser},context_instance=RequestContext(request))
        else:
            response = 0
            return render_to_response('rsvp.html',{'form':form,'response':response},context_instance=RequestContext(request))
    else:
        form = RSVPForm()
        response = 2
        return render_to_response('rsvp.html',{'form':form,'response':response},context_instance=RequestContext(request))
 
 
def apply(request):
    response = 0
    if request.method == 'POST':
        form = AppForm(request.POST, request.FILES)
        if form.is_valid():
            newapp = AppModel()
            newapp.firstname = form.cleaned_data['firstname']
            newapp.lastname = form.cleaned_data['lastname']
            newapp.email = form.cleaned_data['email']
            newapp.living_group = form.cleaned_data['living_group']
            newapp.course = form.cleaned_data['course']
            newapp.year = form.cleaned_data['year']
            newapp.resume = request.FILES['resume']
            newapp.title = form.cleaned_data['title']
            newapp.abstract=form.cleaned_data['abstract']
            newapp.sup = form.cleaned_data['sup']
            
            newapp.audio = request.FILES['audio']
                    
            newapp.datestamp = datetime.datetime.now()
            
            newapp.save()
            
            response = 1
            return render_to_response('app.html',{'form':form,'response':response,'app':newapp},context_instance=RequestContext(request))
        else:
            response = 0
            return render_to_response('app.html',{'form':form,'response':response},context_instance=RequestContext(request))
    else:
        form = AppForm()
        response = 2
        return render_to_response('app.html',{'form':form,'response':response},context_instance=RequestContext(request))
 
     
    
def download_handler_res(request, pk):
    if request.user.is_authenticated():
        upload = get_object_or_404(AppModel, pk=pk)
        return serve_file(request, upload.resume) 
    else:
        return HttpResponseRedirect(reverse('timtalks.main.views.app_list')) 

def download_handler_aud(request, pk):
    if request.user.is_authenticated():
        upload = get_object_or_404(AppModel, pk=pk)
        return serve_file(request, upload.audio, save_as=True)   
    else:
        return HttpResponseRedirect(reverse('timtalks.main.views.app_list')) 
    
def app_list(request):
    if request.user.is_authenticated():
        applications = AppModel.objects.all()
        return render_to_response('applications.html', {'applications':applications},context_instance=RequestContext(request))
    else: 
        def errorHandle(error):
            form= LoginForm()
            return render_to_response('login.html', {
                                                         'error': error,
                                                         'form': form,'page':'home'},context_instance=RequestContext(request))
        if request.method == 'POST':
            form = LoginForm(request.POST)
            if form.is_valid():
                username = request.POST['username']
                password = request.POST['password']
                user = authenticate(username=username,password=password)
                if user is not None:
                        if user.is_active:
                                login(request,user)
                                return HttpResponseRedirect(reverse('timtalks.main.views.app_list'))
                        else:
                            error = u'account disabled'
                            return errorHandle(error)
                else:
                        error = u'Invalid Login. Please try again'
                        return errorHandle(error)
            else:    
                error = u'form is invalid'
                return errorHandle(error)
        else:
                form = LoginForm()
                return render_to_response('login.html', {
                                                         'form':form,'page':'home'}, context_instance=RequestContext(request))
def rsvp_list(request):
    if request.user.is_authenticated():
        rsvps = UserRSVP.objects.all()
        count = rsvps.count()
        guest_count = 0
        for guest in rsvps:
            guest_count = guest_count + guest.guest_num
        
        total = count + guest_count
        return render_to_response('rsvps.html', {'rsvps':rsvps,'count':count,'guest_count':guest_count,'total':total},context_instance=RequestContext(request))
    else: 
        def errorHandle(error):
            form= LoginForm()
            return render_to_response('login.html', {
                                                         'error': error,
                                                         'form': form,'page':'home'},context_instance=RequestContext(request))
        if request.method == 'POST':
            form = LoginForm(request.POST)
            if form.is_valid():
                username = request.POST['username']
                password = request.POST['password']
                user = authenticate(username=username,password=password)
                if user is not None:
                        if user.is_active:
                                login(request,user)
                                return HttpResponseRedirect(reverse('timtalks.main.views.rsvp_list'))
                        else:
                            error = u'account disabled'
                            return errorHandle(error)
                else:
                        error = u'Invalid Login. Please try again'
                        return errorHandle(error)
            else:    
                error = u'form is invalid'
                return errorHandle(error)
        else:
                form = LoginForm()
                return render_to_response('login.html', {
                                                         'form':form,'page':'home'}, context_instance=RequestContext(request))
                
def detail(request,pk):
    if request.user.is_authenticated():
        application = AppModel.objects.get(pk=pk) 
        return render_to_response('detail.html', {'app':application},context_instance=RequestContext(request))
    else: 
        return HttpResponseRedirect(reverse('timtalks.main.views.app_list'))
        